import axios from 'axios'
import router from './router' //引用axios
// eslint-disable-next-line no-unused-vars
import { Message, Loading } from 'element-ui';
// 创建axios实例

// axios.defaults.baseURL = process.env.NODE_ENV == 'development' ? process.env.VUE_APP_DEV : process.env.VUE_APP_API;
// axios.defaults.baseURL = process.env.VUE_APP_URL;
axios.defaults.baseURL = window.g.baseURL;
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8'; //配置请求头
// axios.defaults.timeout = 5000; //响应时间

// axios.defaults.baseURL = "/lother"
// axios.defaults.timeout = 6000

// eslint-disable-next-line no-irregular-whitespace
axios.interceptors.request.use(function(config) {　　 // 在发送请求之前做些什么
  config.url = config.url + "?platform=PC"
    if (localStorage.getItem('lang')=='undefined') { 
      // config.url=config.url+"&lang=zh"
      config.headers['Accept-Language'] = "zh-CN";
    } else {
      // config.url=config.url+"&lang="+localStorage.getItem('lang');
      config.headers['Accept-Language'] = localStorage.getItem('lang');
    }
    if (sessionStorage.getItem('token')) {
      config.headers.Authorization = 'Bearer ' + sessionStorage.getItem("token");// ‘Bearer ’ 为行业规范
    }
    return config
}, function(error) {
    return Promise.reject(error)
});
// 添加响应拦截器
axios.interceptors.response.use(function (response) {
    //token 已过期，重定向到登录页面
    if (response.data.code == '401') {
        router.replace({
            path: '/',
            query: { lang:localStorage.getItem('lang')=='undefined'?'zh':localStorage.getItem('lang')}
        })
        sessionStorage.removeItem('token');
        // this.$vux.toast.text('未授权，请重新登录', 'default')
    }
    return response.data
}, function (error) {
      switch (error.response.status) {
        case 400: 
        Message({
          type: 'error',
          message: '请求错误'+error.response.status
        })  
        break;
        case 401: 
        Message({
          type: 'error',
          message: '未授权，请重新登录'+error.response.status
        }) 
        router.replace({
          path: '/',
          query: { lang:localStorage.getItem('lang')=='undefined'?'zh':localStorage.getItem('lang')}
        })
        sessionStorage.removeItem('token'); 
        break;
        case 403: 
        Message({
          type: 'error',
          message: '拒绝访问'+error.response.status
        }) 
        break;
        case 404: 
        Message({
          type: 'error',
          message: '请求出错'+error.response.status
        })
        break;
        case 408: 
        Message({
          type: 'error',
          message: '请求超时'+error.response.status
        }) 
        break;
        case 500: 
        Message({
          type: 'error',
          message: '服务器错误'+error.response.status
        }) 
        break;
        case 501: 
        Message({
          type: 'error',
          message: '服务未实现'+error.response.status
        }) 
        break;
        case 502: 
        Message({
          type: 'error',
          message: '网络错误'+error.response.status
        }) 
        break;
        case 503: 
        Message({
          type: 'error',
          message: '服务不可用'+error.response.status
        }) 
        break;
        case 504: 
        Message({
          type: 'error',
          message: '网络超时'+error.response.status
        }) 
        break;
        case 505: 
        Message({
          type: 'error',
          message: 'HTTP版本不受支持'+error.response.status
        }) 
        break;
        default: 
        Message({
          type: 'error',
          message: '连接出错'+error.response.status
        }) 
      }
  return Promise.reject(error)  
})

export default axios;